package com.neiquan.secondhand.util;

import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelUtil {
	
	public static void fillExcelData(List list ,Workbook wb,String[] headers)throws Exception{
		int rowIndex=0;
		Sheet sheet=wb.createSheet();
		Row row=sheet.createRow(rowIndex++);
		for(int i=0;i<headers.length;i++){
			row.createCell(i).setCellValue(headers[i]);
		}
		Iterator iterator = list.iterator();
		while(iterator.hasNext()){
			Object next = iterator.next();
			row=sheet.createRow(rowIndex++);
			
//				row.createCell(i).setCellValue(rs.getObject(i+1).toString());
			
		}
	}
	
	public static void export(HttpServletResponse response,Workbook wb,String fileName)throws Exception{
		response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO8859-1"));
		response.setContentType("application/ynd.ms-excel;charset=UTF-8");
		OutputStream out=response.getOutputStream();
		HSSFWorkbook hssfWb = (HSSFWorkbook) wb;
//		wb.write(out);
		out.write(hssfWb.getBytes());
		out.flush();
		out.close();
	}

}
